$(function () {
    /* const phone=${sessionScope.Consumer.phone};
     getCart(phone);
       priceBill(phone)
     */
    getCart(17766040934);
    priceBill(17766040934)
})

function getCart(phoneNumber) {
    $.ajax({
        url: "shopCart/" + phoneNumber,
        dataType: "json",
        type: "GET",
        contentType: "application/json; charset=UTF-8",
        success: function (data) {
            let shopCart = data.data;
            $("#shop_cart").empty();
            shopCart.forEach((product, index) => {
                const row = $("<tr></tr>");
                let productPrice = product.productQuantity * product.price;
                let price_id = "productPrice-" + index;
                const removeCell = $(`<td class='product-remove text-start' ><a ><i class='la la-remove' onclick='removeFromShopCart(${product.id})'></i></a></td>`);
                const thumbnailCell = $(`<td class='product-thumbnail text-start'><img alt='Product Thumnail' src='senshi_official_website/static/picture/${product.productImage}' width="70%" height="88%"><h6><strong style="padding-left: 13% ">${product.productName}</strong></h6></td>`);
                const sizeName = $(`<td ><span class='product-price-wrapper'><span class='money'>${product.sizeName}</span></span></td>`);
                const priceCell = $(`<td class='product-price'><span class='product-price-wrapper'><span class='money'>￥${product.price}</span></span></td>`);
                const quantityCell = $(`<td class='product-quantity'>
<div >
<input   onblur="count_price(${product.price},this,${index},${product.id})" class=' quantity-input'  min='1'  type='number' value='${product.productQuantity}'>
</div>
</td>`);
                const totalCell = $(`<td class='product-total-price'><span class='product-price-wrapper'><span class='money' id="${index}">￥${productPrice}</span></span></td>`);

                row.append(removeCell);
                row.append(thumbnailCell);
                row.append(sizeName);
                row.append(priceCell);
                row.append(quantityCell);
                row.append(totalCell);
                $("#shop_cart").append(row);


            })
        }

    })
}

function count_price(price, quantity, id, product_id) {
    let data = {productQuantity: quantity.value, id: product_id}
    $.ajax({
        url: "shopCart/updateShopCart",
        dataType: "json",
        contentType: "application/json; charset=UTF-8",
        type: "PUT",
        data: JSON.stringify(data),
        success: function (response) {
            if (response.code !== 200) {
                alert(response.msg)
            } else {
                //priceBill(phone)
                priceBill(17766040934)
            }

        },
        error: function () {
            alert("出错了")
        }
    })
    let productPrice = price * quantity.value
    $("#" + id).html("￥" + productPrice)

}

function removeFromShopCart(id) {
    alert(id)
    $.ajax({
        url: "shopCart/" + id,
        dataType: "json",
        type: "DELETE",
        contentType: "application/json; charset=UTF-8",
        success: function (response) {
            if (response.code !== 200) {
                alert(response.msg)
            } else {
                // getCart(phone)  priceBill(phone)
                getCart(17766040934)
                priceBill(17766040934)
            }
        }, error: function () {
            alert("出错了")
        }
    })
}

function emptyShopCart(phone) {
    $.ajax({
        url: "shopCart/emptyShopCart/" + phone,
        dataType: "json",
        type: "DELETE",
        contentType: "application/json; charset=UTF-8",
        success: function (response) {
            if (response.code !== 200) {
                alert(response.msg)
            } else {
                // getCart(phone)  priceBill(phone)
                getCart(17766040934)
                priceBill(17766040934)
            }

        }
    })
}

function priceBill(phone) {
    $.ajax({
        url: "shopCart/bill/" + phone,
        dataType: "json",
        type: "get",
        contentType: "application/json; charset=utf-8",
        success: function (response) {
            console.log(response);

            if (response.data !== "尚未选购商品") {
                $("#subtotal").html("￥" + response.data)
                $("#TotalPrice").html("￥" + response.data + "和邮寄费")
            } else {
                $("#TotalPrice").html(response.data)
                $("#subtotal").html(response.data)
            }
        }
    })
}